//
//  SettingViewController.m
//  笔记1
//
//  Created by  on 2022/11/5.
//

#import "SettingViewController.h"
#import "Setting.h"
#import "PrivateViewController.h"
#import "FeedBackViewController.h"
#import "LoginViewController.h"
#import "FavoriteViewController.h"

@interface SettingViewController ()
@property (strong, nonatomic) IBOutlet UIButton *loginBtn;
@property (strong, nonatomic) IBOutlet UIButton *cancelBtn;
@property (strong, nonatomic) NSArray *array;
@end

@implementation SettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadData];
    [self initUI];
    [NotifyTool add:self :@selector(loadData) :notifyUserUpdate];
}
//- (void)viewWillAppear:(BOOL)animated{
//    [super viewWillAppear:animated];
//    [self.navigationController setNavigationBarHidden:YES animated:animated];
//}
//- (void)viewWillDisappear:(BOOL)animated{
//    [super viewWillDisappear:animated];
//    [self.navigationController setNavigationBarHidden:NO animated:animated];
//}
- (void)initUI
{
//    self.backBtn.hidden = YES;
    self.array = @[@"收藏",@"废纸篓",@"清理缓存",@"帮助与反馈",@"隐私政策"];
    self.tableViewStyle = UITableViewStyleGrouped;
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectZero];
    self.tableView.backgroundColor = [@"#F8D1B4" color];
    self.tableView.scrollEnabled = NO;
    self.tableView.c_top.equalTo(self.view).coffset(70);
    self.tableView.c_left.equalTo(self.view).coffset(20);
    self.tableView.c_right.equalTo(self.view).coffset(-20);
    self.tableView.c_height.c_equal(60 * self.array.count + 20);
    self.tableView.layer.masksToBounds = NO;
    self.tableView.layer.shadowColor = [UIColor colorHex:@"#9B9B9B"].CGColor;
    self.tableView.layer.shadowOffset = CGSizeMake(0, 3);
    self.tableView.layer.shadowRadius = 12;
    self.tableView.layer.cornerRadius = 12;
    self.tableView.layer.shadowOpacity = 1;
    [self.tableView registerNib:[Setting nib] forCellReuseIdentifier:[Setting ID]];
}
- (void)loadData{
    self.cancelBtn.hidden = [APP.user.account isEqualToString:@"去登录"];
    NSString *title = self.cancelBtn.hidden ? @"去登录":@"退出登录";
    [self.loginBtn setTitle:LS(title) forState:UIControlStateNormal];
    [self.tableView reloadData];
}
- (IBAction)actionForBtn:(UIButton *)sender {
    if ([sender.currentTitle isEqualToString:LS(@"去登录")]) {
        LoginViewController * vc = [LoginViewController new];
        vc.islogin = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }else if([sender.currentTitle isEqualToString:LS(@"退出登录")]){
        APP.user = [User new];
    }else if([sender.currentTitle isEqualToString:LS(@"注销账号")]){
        [self alert:LS(@"注销账号") :LS(@"您确定要注销账号吗？") :^{
        } :^{
            [UserTool delete:APP.user complete:^(NSDictionary * _Nonnull dict) {
                int code = [dict[@"code"] intValue];
                if (code == 200) {
                    APP.user = [User new];
                }
            }];
        }];
    }
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Setting *cell = [tableView dequeueReusableCellWithIdentifier:[Setting ID]];
    cell.name.text = LS(self.array[indexPath.row]);
    cell.img.image = [UIImage imageNamed:self.array[indexPath.row]];
    return cell;
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 20;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 20)];
    view.backgroundColor = [UIColor clearColor];
    return view;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *title = self.array[indexPath.row];
    if ([title isEqualToString:@"收藏"]) {
        FavoriteViewController *vc = [FavoriteViewController new];
        vc.isFavorite = YES;
        [self push:vc :YES];
    }else if ([title isEqualToString:@"废纸篓"]){
        FavoriteViewController *vc = [FavoriteViewController new];
        vc.isFavorite = NO;
        [self push:vc :YES];
    }else if ([title isEqualToString:@"清理缓存"]){
        [CCHud startHud:LS(@"清理缓存中...") stopTime:2 handle:^{
            [CCHud msg:LS(@"已清理干净")];
        }];
    }else if ([title isEqualToString:@"帮助与反馈"]){
        FeedBackViewController * vc= [FeedBackViewController new];
        vc.title = LS(@"帮助与反馈");
        [self push:vc :YES];
    }else if ([title isEqualToString:@"隐私政策"]){
        PrivateViewController * vc= [PrivateViewController new];
        [self push:vc :YES];
    }
}

@end

